﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class KeyboardInput : IUserInput {

    [Header("===== Key settings =====")]
    public string keyUp = "w";
    public string keyDown = "s";
    public string keyLeft = "a";
    public string keyRight = "d";

    public string keyA = "left shift";
    public string keyB = "space";
    public string keyC = "mouse 0";
    public string keyD = "mouse 1";

    public string keyJRight = "right";
    public string keyJLeft = "left";
    public string keyJUp = "up";
    public string keyJDown = "down";

    [Header("===== Mouse settings =====")]
    public bool mouseEnable = true;
    public float mouseSensitivityX = 1.0f;
    public float mouseSensitivityY = 1.0f;
    //[Header("===== Output signal =====")]
    //public float Dup;
    //public float Dright;
    //public float Dmag;
    //public Vector3 Dvec;

    //public float Jup;
    //public float Jright;

    ////1. pressing signal
    //public bool run;
    ////2. trigger once signal
    //public bool jump;
    //private bool lastJump;
    //public bool attack;
    //private bool lastAttack;
    ////3. double trigger
    //[Header("===== Others =====")]
    //public bool inputEnable = true;

    //private float targetDup;
    //private float targetDright;
    //private float velocityDup;
    //private float velocityDright;


    // Update is called once per frame
    void Update () {
        if (mouseEnable)
        {
            Jup = Input.GetAxis("Mouse Y") * 2.0f * mouseSensitivityX;
            Jright = Input.GetAxis("Mouse X") * 2.0f * mouseSensitivityY;
        }
        else
        {
            Jup = (Input.GetKey(keyJUp) ? 1.0f : 0) - (Input.GetKey(keyJDown) ? 1.0f : 0);
            Jright = (Input.GetKey(keyJRight) ? 1.0f : 0) - (Input.GetKey(keyJLeft) ? 1.0f : 0);
        }
        //自定义纵横轴输入信号
        targetDup = (Input.GetKey(keyUp) ? 1.0f : 0) - (Input.GetKey(keyDown) ? 1.0f : 0);
        targetDright = (Input.GetKey(keyRight) ? 1.0f : 0) - (Input.GetKey(keyLeft) ? 1.0f : 0);

        if (!inputEnable)
        {
            targetDup = 0;
            targetDright = 0;

        }
        Dup = Mathf.SmoothDamp(Dup, targetDup, ref velocityDup, 0.1f);
        Dright = Mathf.SmoothDamp(Dright, targetDright, ref velocityDright, 0.1f);

        Vector2 tmepDAxis = SquareToCircle(new Vector2(Dright, Dup));
        Dmag =  new Vector2(tmepDAxis.y, tmepDAxis.x).magnitude;

        Dvec = Dright * transform.right + Dup * transform.forward;

        run = Input.GetKey(keyA);

        defense = Input.GetKey(keyD);

        bool newJump = Input.GetKey(keyB);
        jump = (newJump != lastJump && newJump == true) ? true : false;
        lastJump = newJump;

        bool newAttack = Input.GetKey(keyC);
        attack = (newAttack != lastAttack && newAttack == true) ? true : false;
        lastAttack = newAttack;
    }

    //private Vector2 SquareToCircle(Vector2 input)
    //{
    //    Vector2 output = Vector2.zero;

    //    output.x = input.x * Mathf.Sqrt(1 - (input.y * input.y) / 2.0f);
    //    output.y = input.y * Mathf.Sqrt(1 - (input.x * input.x) / 2.0f);
    //    return output;
    //}

}
